<?php

/**
 * This is the model class for table "{{passports}}".
 *
 * The followings are the available columns in table '{{passports}}':
 * @property integer $id
 * @property integer $client_id
 * @property integer $series
 * @property integer $number
 * @property string $issued_by
 * @property string $issued_date
 * @property integer $subdivision_code
 * @property integer $type
 * @property string $created_at
 * @property string $updated_at
 */
class Passports extends CActiveRecord
{
	public $is_passport;

	const TYPE_CURRENT = 1;
	const TYPE_PREV = 0;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Passports the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{passports}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('client_id, series, number, created_at, type', 'required'),
			array('issued_by, issued_date, subdivision_code', 'required', 'on'=>'currentPassport'),
			array('is_passport','checkPassport'),
			array('client_id, series, number, subdivision_code, type', 'numerical', 'integerOnly'=>true),
			array('issued_by', 'length', 'max'=>255),
			array('issued_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('client_id, series, number, type', 'safe', 'on'=>'search'),
		);
	}

	public function checkPassport()
	{
		$criteria = new CDbCriteria();
		$criteria->addCondition('series=:s AND number=:n AND id!=:i');
		$criteria->params = array(
			':s'=>$this->series,
			':n'=>$this->number,
			':i'=>$this->id
		);

		if(Passports::model()->find($criteria)!==null){
			$this->addError('is_passport','Паспорт с такой серией и номером уже есть в базе.');
		}
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	public function behaviors(){
		return array(
			'createUpdateBehavior' => array(
				'class' => 'application.behaviors.CreateDateBehavior',
				'createdField' => 'created_at'
			)
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'client_id' => 'Клиент',
			'series' => 'Серия',
			'number' => 'Номер',
			'issued_by' => 'Кем выдан',
			'issued_date' => 'Дата выдачи',
			'subdivision_code' => 'Код подразделения',
			'type' => 'Тип',
			'created_at' => 'Created At',
			'updated_at' => 'Updated At',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('client_id',$this->client_id);
		$criteria->compare('series',$this->series);
		$criteria->compare('number',$this->number);
		$criteria->compare('issued_by',$this->issued_by,true);
		$criteria->compare('issued_date',$this->issued_date,true);
		$criteria->compare('subdivision_code',$this->subdivision_code);
		$criteria->compare('type',$this->type);
		$criteria->compare('created_at',$this->created_at,true);
		$criteria->compare('updated_at',$this->updated_at,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}